﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Management.Automation; // Windows PowerShell namespace 
using System.Management.Automation.Runspaces; // Windows PowerShell namespace 
using System.Security; // For the secure password 


namespace Erlacher.Opalis.Virsto
{
    public static class PowerShellHelper
    {
        public class ReturnObject
        {
 
            private string i_ErrorState = "0";
            private String s_ErrorMessage = string.Empty;
            private String s_Trace = string.Empty;
            private String s_value = string.Empty;

            public string ErrorState
            {
                get { return i_ErrorState; }
                set { i_ErrorState = value; }
            }

            public String ErrorMessage
            {
                get { return s_ErrorMessage; }
                set { s_ErrorMessage = value; }
            }

            public String Trace
            {
                get { return s_Trace; }
                set { s_Trace = value; }
            }

            public String Value
            {
                get { return s_value; }
                set { s_value = value; }
            }

        }
        private static void openRunspace(string uri, string schema, string username, string livePass, ref Runspace remoteRunspace)
        {
            System.Security.SecureString password = new System.Security.SecureString();
            foreach (char c in livePass.ToCharArray())
            {
                password.AppendChar(c);
            }
            PSCredential psc = new PSCredential(username, password);
            WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);
            rri.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
            rri.ProxyAuthentication = AuthenticationMechanism.Negotiate;
            remoteRunspace = RunspaceFactory.CreateRunspace(rri);
            remoteRunspace.Open();
        }

        public static ReturnObject RunScript(string scriptText, string username, string livePass, string servername)
        {

            //if (alternate_servername != "[USE DEFAULT SETTINGS]")
            //{
            //    servername = alternate_servername;
            //}
            //if (alternate_username != "[USE DEFAULT SETTINGS]")
            //{
            //    username = alternate_username;
            //}
            //if (alternate_livePass != "[USE DEFAULT SETTINGS]")
            //{
            //    livePass = alternate_livePass;
            //}

            ReturnObject returns = new ReturnObject();

            PSCredential credential = null;
            credential = new PSCredential(username, Helper.CreateSecurePassword(livePass));

            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, servername, 80, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;



            using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                remoteRunspace.Open();



                using (PowerShell powershell = PowerShell.Create())
                {
                    powershell.Runspace = remoteRunspace;
                    powershell.AddScript(scriptText);

                   
                    Collection<PSObject> results = powershell.Invoke();

                    if (results.Count == 4)
                    {
                        returns.ErrorState = results[0].ToString();
                        returns.ErrorMessage = results[1].ToString();
                        returns.Trace = results[2].ToString();
                        returns.Value = results[3].ToString();
                    }
                    else if (results.Count == 5)
                    {
                        returns.ErrorState = results[1].ToString();
                        returns.ErrorMessage = results[2].ToString();
                        returns.Trace = results[3].ToString();
                        returns.Value = results[4].ToString();
                    }
                    else
                    {
                        returns.ErrorState = "1";
                        returns.ErrorMessage = "Return Counter not match: " + results.Count.ToString();
                        returns.Trace = results[0].ToString();
                    }

                }
                remoteRunspace.Close();
            }
                return returns;
         }

    }
}
